package com.atguigu.day2;

/*
Day2 三个窗口卖50张票练习
    线程lambda版
    口诀 高内聚低耦合前提下，线程 操作 资源类
* */

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//资源类Ticket
class Ticket {
    private int number = 50;
    Lock lock = new ReentrantLock();


    public void saleTicket() {
        lock.lock();
        try {
            if (number > 0) {
                System.out.println(Thread.currentThread().getName() + "---->已卖出第" + (number--) + "\t还剩下" + number);

            }
        } finally {
            lock.unlock();
        }
    }
}

public class SaleTicket {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();

        /*拷贝小括号，写死右箭头，落地大括号*/
        new Thread(() -> {
            for (int i = 0; i < 50; i++) {
                ticket.saleTicket();
            }
        }, "A").start();
        new Thread(() -> {
            for (int i = 0; i < 50; i++) {
                ticket.saleTicket();
            }
        }, "B").start();
        new Thread(() -> {
            for (int i = 0; i < 50; i++) {
                ticket.saleTicket();
            }
        }, "C").start();

    }

}
